﻿class ProductsController < ApplicationController
  # GET /products
  # GET /products.json
  def index
    @title = "#{t :DrinkTitle}"
    @products1 = Product.paginate :page=>params[:page], :order=>'product_id ASC', :per_page => 7
    
  
    
    #search by name of drink
    if(params[:searchDrink]!=nil)
      @products = @products1.searchDrink(params[:searchDrink])
    end
    #end search by name of drink

    #Search by Category
    if(params[:searchCategory]!=nil)
      @aa = params[:searchCategory]
      @bb = Category.find(:first, :conditions => ['Name LIKE ?', "#{@aa}"])
      if(@bb!=nil)
        @cc = @bb.category_id
        @products = @products1.searchCategory(@cc)
      else
        @products = @products1.find(:all, :conditions=>['Name LIKE?', "#{23582323227874878}"]) #return NULL
      end
    end
    #End Search by Category

   

    #Other: NOT search OR NOT filter
    if(params[:searchDrink]==nil && params[:searchCategory]==nil)
      @products = @products1
    end
    #end
    @category = Category.all
    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @products, json: @category }
    end
  end

  # GET /products/1
  # GET /products/1.json
  def show
    @product = Product.find(params[:id])
    @title = @product.Name

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @product }
    end
  end

  # GET /products/new
  # GET /products/new.json
  def new
    @product = Product.new
    @title = "#{t :DrinkAdd}"
    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @product }
    end
  end

  # GET /products/1/edit
  def edit
    @title = "#{t :DrinkEdit}"
    @product = Product.find(params[:id])
  end

  # POST /products
  # POST /products.json
  def create
    @product = Product.new(params[:product])
    respond_to do |format|
      if @product.save
        format.html { redirect_to @product, notice: "#{t :insertSuc}" }
        format.json { render json: @product, status: :created, location: @product }
      else
        format.html { render action: "new" }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /products/1
  # PUT /products/1.json
  def update
    @product = Product.find(params[:id])

    respond_to do |format|
      if @product.update_attributes(params[:product])
        format.html { redirect_to @product, notice: "#{t :updateSuc}" }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /products/1
  # DELETE /products/1.json
  def destroy
    @product = Product.find(params[:id])
    @product.destroy

    respond_to do |format|
      format.html { redirect_to products_url }
      format.json { head :no_content }
    end
  end
  
  def require
    
  end
  def require_post
    @title = "#{t :requireR}"
  @aa = "Yêu cầu của bạn đã được gửi, cảm ơn đóng góp của bạn"
  RequireBookMail.require_book_mail(params["body"]).deliver
    respond_to do |format|
      format.html { redirect_to "/products/require", notice: @aa }
      format.json { render json: @aa}
    end
  end
  
end
